package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsNotice;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyRsNoticeMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyRsNoticeService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 审评查验-公告
 * @Author: yuncheng
 * @Date: 2023-11-08
 * @Version: V1.0
 */
@Service
public class SpcyRsNoticeServiceImpl extends BaseServiceImpl<SpcyRsNoticeMapper, SpcyRsNotice> implements ISpcyRsNoticeService {

    private static final Logger log = LoggerFactory.getLogger(SpcyRsNoticeServiceImpl.class);

    @Resource
    private ISpcyLogService spcyLogService;

    @Override
    public IPage<SpcyRsNotice> queryPageNotice(SpcyRsNotice notice,
                                               String startTime, String endTime,
                                               Integer pageNo, Integer pageSize,
                                               OrderItemVo orderItemVo) {
        IPage<SpcyRsNotice> page = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<SpcyRsNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(notice.getFggbt()), SpcyRsNotice::getFggbt, notice.getFggbt());
        queryWrapper.like(StringUtils.isNotBlank(notice.getFggnr()), SpcyRsNotice::getFggnr, notice.getFggnr());
        queryWrapper.like(StringUtils.isNotBlank(notice.getFyxzt()), SpcyRsNotice::getFyxzt, notice.getFyxzt());
        queryWrapper.like(StringUtils.isNotBlank(notice.getFsfzd()), SpcyRsNotice::getFsfzd, notice.getFsfzd());
        queryWrapper.like(StringUtils.isNotBlank(notice.getFfbrmc()), SpcyRsNotice::getFfbrmc, notice.getFfbrmc());

        if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)){
            queryWrapper.apply("to_char(ffbsj,'yyyy-mm-dd') >= {0}",startTime);
        }else if (StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)){
            queryWrapper.apply("to_char(ffbsj,'yyyy-mm-dd') <= {0}", endTime);
        }else if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            queryWrapper.apply("to_char(ffbsj,'yyyy-mm-dd') >= {0} and to_char(ffbsj,'yyyy-mm-dd') <= {1}",startTime, endTime);
        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByAsc(SpcyRsNotice::getFsfzd);
            queryWrapper.orderByDesc(SpcyRsNotice::getFfbsj);
            queryWrapper.orderByDesc(SpcyRsNotice::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SpcyRsNotice> pageList = page(page, queryWrapper);

        return pageList;
    }

    @Override
    public SpcyRsNotice addNotice(SpcyRsNotice notice) {
        if (StringUtils.isBlank(notice.getFggbt())){
            throw new BusinessException("标题不能为空！");
        }

        if (StringUtils.isBlank(notice.getFggnr())){
            throw new BusinessException("内容不能为空！");
        }

        if (StringUtils.isBlank(notice.getFsfzd())){
            notice.setFsfzd(SpcyCommonConstant.NOTICE_SFZD_YES);
        }
        notice.setFyxzt(SpcyCommonConstant.NOTICE_YXZT_YX);
        notice.setFfbsj(new Date());

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        notice.setFfbrid(currentUser.getId());
        notice.setFfbrmc(currentUser.getName());

        save(notice);

        return notice;
    }

    @Override
    public void updateNotice(HashMap<String, Object> paramMap) {
        String id = paramMap.get("id").toString();
        SpcyRsNotice updateNotice = getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateNotice, paramMap);
        } catch (IllegalAccessException e) {
            log.error("更新公告出错：[IllegalAccessException]->{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新公告出错：[InvocationTargetException]->{}",e.getMessage());
        }
        updateById(updateNotice);
    }

    @Override
    public void deleteNotice(ParamsBo paramsBo) {
        if (StringUtils.isBlank(paramsBo.getIds())){
            throw new BusinessException("请选择数据！");
        }


        List<String> idsList = StringPatternUtils.getListByIds(paramsBo.getIds());
        List<SpcyRsNotice> noticeList = listByIds(idsList);
        if (CollectionUtil.isNotEmpty(noticeList)) {
            for (SpcyRsNotice notice : noticeList) {
                notice.setFyxzt(SpcyCommonConstant.NOTICE_YXZT_WX);
                updateById(notice);

                spcyLogService.addLogs(LogConstant.LOGS_NOTICE, "删除公告："+notice.getFggbt(),notice.getId(), "删除公告",SpcyConstant.LOGS_SYS_TYPE_SC);
            }
        }
    }

    @Override
    public SpcyRsNotice queryById(String id) {
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        SpcyRsNotice notice = getById(id);

        return notice;
    }
}
